<template>
    <!-- 新增、修改质控点 -->
    <div class="standard-dialog">
        <el-dialog
                :title="dialogParams.title"
                :visible.sync="dialogParams.visible"
                :close-on-click-modal="false"
                width="80%">
            <el-form
                    :model="formData"
                    :rules="rules"
                    :disabled="dialogParams.type === 'detail'"
                    ref="formData"
                    label-width="120px"
                    label-position="right"
                    class="point-form">
                <!-- 基础信息 -->
                <div>
                    <div class="title">基础信息</div>
                    <div class="form_flex">
                        <el-form-item label="一级指标" prop="indexOne">
                            <el-select v-model="formData.indexOne" filterable placeholder="请选择" disabled>
                                <el-option
                                        v-for="item in indexOneArr"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.name">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="二级指标" prop="indexTwo">
                            <el-input v-model="formData.indexTwo" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="三级指标" prop="indexThree">
                            <el-input v-model="formData.indexThree" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="质控点" prop="name">
                            <el-input v-model="formData.name" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="质控点内涵" prop="intension">
                            <el-input v-model="formData.intension" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="诊断标准" prop="diagnosisStandard">
                            <el-input v-model="formData.diagnosisStandard" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="质控点类别" prop="chargeCategory">
                            <el-select v-model="formData.chargeCategory" filterable placeholder="请选择" disabled>
                                <el-option
                                        v-for="item in pointCategoryArr"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.code">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="质控点类型" prop="chargeType">
                            <el-select v-model="formData.chargeType" filterable placeholder="请选择" disabled>
                                <el-option
                                        v-for="item in pointTypeArr"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.code">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="值类型" prop="valueType">
                            <el-select v-model="formData.valueType" placeholder="请选择" disabled>
                                <el-option
                                        v-for="item in pointValueTypeArr"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.code">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="预期值" prop="expectedValue">
                            <el-select v-model="formData.expectedValue" placeholder="请选择值预期" disabled>
                                <el-option v-for="(item,index) in expectedValueArr" :key="index" :label="item.name" :value="item.code"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="权重" prop="weight">
                            <el-input v-model="formData.weight" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="责任部门" prop="belongPart">
                            <el-select v-model="formData.belongPart" filterable clearable placeholder="请选择" disabled>
                                <el-option
                                        v-for="item in organizeArr"
                                        :key="item.id"
                                        :label="item.orgName"
                                        :value="item.id">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </div>
                </div>
                <div>
                    <div class="title">标准值</div>
                    <div class="form_flex">
                        <el-form-item label="标准值" prop="standardValue">
                            <el-input v-model="formData.standardValue" style="width: 120px!important;margin-right: 10px"></el-input>
                            <el-button type="primary" @click="handleValue">值同步</el-button>
                        </el-form-item>
                        <el-form-item label="标准值同步对象" prop="sourceStandardValue">
                            <el-input v-model="formData.sourceStandardValue" disabled></el-input>
                        </el-form-item>
                        <el-form-item label="标准值同步指标" prop="sourcePointName">
                            <el-input v-model="formData.sourcePointName" disabled></el-input>
                        </el-form-item>
                    </div>
                </div>
                <!-- 标准值 -->
                <div v-if="activeStandard == 2">
                    <div v-for="(item,index) in subRectificationRangeList" :key="index">
                        <div class="title">
                            <span>{{ `标准值-${item.name}` }}</span>
                            <div class="value-box" :style="{ 'display': dialogParams.editFlag ? '' : 'none' }">
                                <el-input v-model="item.standardValue" placeholder="请输入标准值"></el-input>
                                <el-button type="primary" @click="handleValue(index)">值同步</el-button>
                                <el-button type="primary" @click="quickStandardValue(index, item.standardValue)">一键设置</el-button>
                            </div>
                        </div>
                        <div class="form_flex">
                            <el-table
                                    :key="index"
                                    :data="item.subList"
                                    style="width: 100%;">
                                <el-table-column
                                        prop="rectificationObjectName"
                                        label="诊改对象">
                                </el-table-column>
                                <el-table-column
                                        prop="name"
                                        label="质控点名称">
                                </el-table-column>
                                <el-table-column label="标准值">
                                    <template slot-scope="scope">
                                        <div class="value-box">
                                            <el-input v-model="scope.row.standardValue" placeholder="请输入标准值"></el-input>
                                            <el-button type="primary" @click="handleSubValue(index, scope.$index)" :style="{ 'display': dialogParams.editFlag ? '' : 'none' }">值同步</el-button>
                                        </div>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </div>
                    </div>
                </div>
            </el-form>

            <span slot="footer" class="dialog-footer" :style="{ 'display': dialogParams.editFlag ? '' : 'none' }">
                <el-button @click="dialogParams.visible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('formData')">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 值同步 -->
        <valueSync
                v-if="valueParams.visible"
                :dialog-params="valueParams"
                @changeStandardValue="changeStandardValue"></valueSync>
    </div>
</template>

<script>
    import valueSync from '@/views/targetSystem/standardManage/module/valueSync.vue'
    import {getStorage} from "@/config/utils";
    import {getAllOrganize} from "@/api/systemManage";
    import {updateStandardValue, getValueSyncPointList} from "../../../../api/target";
    import {updateWaitDealState} from "../../../../api";
    import {getStrByRangeId} from "@/api/system";

    export default {
        name: 'setStandardValue',
        components: {
            valueSync
        },
        props: {
            dialogParams: {
                type: Object
            }
        },
        computed: {
            pointTypeArr() {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.pointType);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            },
            pointCategoryArr () {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.pointCategory);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            },
            pointValueTypeArr() {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.pointValueType);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            },
            indexOneArr() {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.xtWZ);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            },
            expectedValueArr() {
                let systemInfo = getStorage('systemInfo');
                if (systemInfo && systemInfo.length) {
                    let temp = systemInfo.filter(item => item.code === this.$config.pointZYQ);
                    if (temp && temp.length) {
                        return temp[0].dictValDTOList
                    }
                }
                return []
            }
        },
        data() {
            return {
                formData: {
                    indexOne: '',
                    indexTwo: '',
                    indexThree: '',
                    chargeType: '',
                    chargeCategory: '',
                    intension: '',
                    diagnosisStandard: '',
                    name: '',
                    expectedValue: '',
                    standardValue: '',
                    weight: '',
                    belongPart: '',

                    sourceStandardValue: '', //标准值同步对象
                    sourcePointName: '', //标准值同步指标
                },
                allAlgorithmArr: [],
                allAlgorithmVersionArr: [],
                allUser: [],
                algorithmDesc: '', // 算法说明，选择算法名称后自动回显
                rules: {
                    indexOne: [
                        {required: true, message: '请输入一级指标', trigger: 'blur'}
                    ],
                    indexTwo: [
                        {required: true, message: '请输入二级指标', trigger: 'blur'}
                    ],
                    chargeType: [
                        {required: true, message: '请选择质控点类型', trigger: 'change'}
                    ],
                    algorithmId: [
                        {required: true, message: '请选择算法名称', trigger: 'change'}
                    ],
                    intension: [
                        {required: true, message: '请输入质控点内涵', trigger: 'blur'}
                    ],
                    diagnosisStandard: [
                        {required: true, message: '请输入诊断标准', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入质控点', trigger: 'blur'}
                    ],
                    valueType: [
                        {required: true, message: '请选择值类型', trigger: 'change'}
                    ],
                    expectedValue: [
                        {required: true, message: '请输入预期值', trigger: 'change'}
                    ],
                    weight: [
                        {required: true, message: '请输入权重', trigger: 'blur'}
                    ],
                    belongPart: [
                        {required: true, message: '请选择责任部门'}
                    ]
                },
                // 责任部门
                organizeArr: [],
                valueParams: {
                    visible: false,
                    title: '值同步',
                    data: {},
                    index: '', // 第几个标准值的值同步选中的标准值
                    subIndex: '' // 子级第几个标准值的值同步选中的标准值
                },
                // 弹框值同步所选的标准值相关数据
                sourcePoint: {},
                // 根据适用范围回显的诊改对象
                rectificationObject: [],
                // activeStandard
                activeStandard: 1, // 标准值模式：1表示在该角度下无多个诊该对象 ，2表示有多个
                // 三级质控点列表
                subRectificationRangeList: [],
                // 值同步
                subRectification: [],
                column: [
                    {
                        prop: 'rectificationObjectName',
                        label: '诊改对象'
                    },
                    {
                        prop: 'name',
                        label: '质控点名称'
                    }
                ]
            };
        },
        async created () {
            this.getOrganize();

            // 根据所有角度查询诊改对象
            if (this.dialogParams.rectificationRangeId) {
                this.changeRange()
            }

            // 查询质控点列表
            this.getPointList()
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.setStandardValue(this.sourcePoint)
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            // 获取责任部门下拉数据
            getOrganize() {
                getAllOrganize().then(res => {
                    this.organizeArr = res || []
                }).catch()
            },
            // 打开值同步
            handleValue (index) {
                this.valueParams.visible = true
                this.valueParams.index = index
                this.valueParams.subIndex = ''
            },
            // 打开子级值同步
            handleSubValue (index, subIndex) {
                this.valueParams.visible = true
                this.valueParams.subIndex = subIndex
                this.valueParams.index = index
                // console.log(index, subIndex);
            },
            // 选取需要同步的质控点的相关信息
            changeStandardValue (data) {
                this.formData.sourceStandardValue = data.standardValue
                this.formData.sourcePointName = data.name
                this.formData.standardValue = data.standardValue

                this.sourcePoint = data

                // 如果有下一级的诊改对象
                let index = this.valueParams.index
                let subIndex = this.valueParams.subIndex
                if (index === 0 || index) {
                    if (subIndex === 0 || subIndex) {
                        this.$set(this.subRectificationRangeList[index].subList[subIndex], 'standardValue', data.standardValue)
                        this.$set(this.subRectificationRangeList[index].subList[subIndex], 'standardValueSourceId', data.standardValueSourceId)
                        // this.subRectificationRangeList[index].subList[subIndex].standardValue = data.standardValue
                        // this.subRectificationRangeList[index].subList[subIndex].standardValueSourceId = data.targetStandardId
                    } else {
                        this.subRectificationRangeList[index].standardValue = data.standardValue
                        this.subRectificationRangeList[index].standardValueSourceId = data.targetStandardId
                    }
                }

            },
            // 修改标准值
            setStandardValue(data) {
                let params = {}
                if (this.activeStandard == 1) {
                    params = {
                        id: this.dialogParams.id,
                        standardValue: data.standardValue || this.formData.standardValue,
                        standardValueSourceId: data.targetStandardId
                    }
                } else if (this.activeStandard == 2) {
                    let subList = []
                    let issueStandardValueList = []
                    this.subRectificationRangeList.forEach((item, index) => {
                        subList = subList.concat(item.subList)
                    })
                    if (subList.length) {
                        subList.forEach(item => {
                            issueStandardValueList.push({
                                id: item.id,
                                standardValue: item.standardValue,
                                standardValueSourceId: item.standardValueSourceId || ''
                            })
                        })
                    }
                    params = {
                        id: this.dialogParams.id,
                        standardValue: data.standardValue || this.formData.standardValue,
                        issueStandardValueList: issueStandardValueList,
                    }
                }

                updateStandardValue(params).then(res => {
                    this.$message.success('操作成功');
                    this.$emit('getList')
                    this.dialogParams.visible = false
                    if (this.dialogParams.isWaitDeal) {
                        updateWaitDealState(this.dialogParams.waitDealId, 1);
                    }
                }).catch(() => {});
            },
            // 根据选中的适用范围回显诊改对象
            changeRange () {
                getStrByRangeId({rangeId: +this.dialogParams.rectificationRangeId}).then(res => {
                    if (res) {
                        this.rectificationObject = res.split(',')
                    }
                }).catch(() => {})
            },
            // 获取质控点列表
            getPointList () {
                getValueSyncPointList({id: this.dialogParams.id}).then(res => {
                    if (res.subRectificationRangeList) {
                        this.subRectificationRangeList = res.subRectificationRangeList
                        this.activeStandard = 2
                    } else {
                        this.activeStandard = 1
                    }
                }).catch(() => {})
            },
            // 一键设置
            quickStandardValue (index, standardValue) {
                let subList = this.subRectificationRangeList[index].subList
                if (subList && subList.length) {
                    subList.forEach((item, i) => {
                        this.$set(this.subRectificationRangeList[index].subList[i], 'standardValue', standardValue)
                    })
                }
            }
        },
        watch: {
            dialogParams:{
                handler (val) {
                    this.formData = {...val.data}
                    if (this.formData.algorithmId) {
                        this.getAllAlgorithmVersion(this.formData.algorithmId)
                    }
                },
                deep: true,
                immediate: true
            }
        }
    }
</script>

<style lang="scss" scoped>
    .standard-dialog {
        .title {
            font-weight: bold;
            font-size: 14px;
            padding: 10px 0;
            margin: 10px;
            border-bottom: 1px solid $lineColor;
        }

        .point-form {
            /*display: flex;*/
            /*flex-wrap: wrap;*/
            .title{
                padding: 10px 0;
                border-bottom: 1px solid #ff860b;
                display: flex;
                justify-content: space-between;
                align-items: center;
            }
            .value-box{
                display: flex;
                .el-input{
                    margin-right: 10px;
                }
            }
            .form_flex{
                display: flex;
                flex-wrap: wrap;
                max-height: 300px;
                overflow-y: auto;
                .el-table{
                    width: 100%;
                }
            }
            .el-form-item {
                width: 33%;
                input, select {
                    width: 100% !important;
                }
            }
        }
    }
</style>